package java_DesignPattern.BehavioralPattern.InterpreterPattern;

/**
 * 加法表达式类
 */
public class Add extends AbstractExpression {

    private AbstractExpression left;
    private AbstractExpression right;

    // 构造方法
    public Add(AbstractExpression left, AbstractExpression right) {
        this.left = left;
        this.right = right;
    }

    /**
     * 解释加法表达式
     */
    @Override
    public int interpret(Context context) {
        return left.interpret(context) + right.interpret(context);
    }

     /**
      * 加法表达式的字符串表示
      */
    @Override
    public String toString() {
        return "(" + left.toString() + " + " + right.toString() + ")";
    }
}
